import 'package:flutter/material.dart';
import 'package:flutter_architecture_samples_inherited_widget/models.dart';
import 'package:todos_app_core/todos_app_core.dart';

class AddEditScreen extends StatefulWidget {
  final Todo? todo;
  const AddEditScreen({this.todo, Key? key}) : super(key: key);

  @override
  State<AddEditScreen> createState() => _AddEditScreenState();
}

class _AddEditScreenState extends State<AddEditScreen> {
  static final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  bool get _isEditing => widget.todo != null;
  String _task = '';
  String _note = '';
  @override
  Widget build(BuildContext context) {
    final localizations = ArchSampleLocalizations.of(context)!;
    return Scaffold(
      body: Scaffold(
        appBar: AppBar(
          actions: [],
          title:
              Text(_isEditing ? localizations.editTodo : localizations.addTodo),
        ),
        body: Padding(
          padding: EdgeInsets.all(16),
          child: Form(
            key: _formKey,
            child: ListView(
              children: [
                TextFormField(
                    initialValue: _isEditing ? widget.todo!.task : '',
                    autofocus: !_isEditing,
                    style: Theme.of(context).textTheme.labelLarge,
                    decoration:
                        InputDecoration(labelText: localizations.newTodoHint),
                    validator: (val) => val!.trim().isEmpty
                        ? ArchSampleLocalizations.of(context)!.emptyTodoError
                        : null,
                    onSaved: (newValue) => _task = newValue!)
              ],
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            final form = _formKey.currentState;
            if (form!.validate()) {
              form.save();
            }
          },
          child: Icon(_isEditing ? Icons.save : Icons.check),
        ),
      ),
    );
  }
}
